document.write("<iframe id=\"HuShuaWebCalender\" name=\"ElanWebCalender\" src=\"/calendar/calendar.html\" style=\"width:379px; height:182px; display:none;  position:absolute; z-index:999999\" frameborder=\"0\" scrolling=\"no\"></iframe>");

var Calendar = {
	Bind : function(element, eventName, calendarParams, call){
		if(!element || !eventName || !calendarParams) return;
		var control = $(element);
		if(!control) return;
		var th = this;
		var eventCalendar = (
			function(e){
				if(e) Event.stop(e);
				th.Show(calendarParams);
				if(call) try{ call.call() } catch(e){};
			}
		);
		
		if((typeof(eventName)).toLowerCase() != "object"){
			eventName = [ eventName.toString() ];
		}
		for(var i = 0; i < eventName.length; i++){
			Event.stopObserving(control, eventName[i]);
			Event.observe(control, eventName[i], eventCalendar);
		}
	},
	
	Show : function(){
		var defaults = {
			handle: null, 
			field: null, 
			nextHandle: null, 
			nextField: null, 
			beginDate: null, 
			endDate: null, 
			week: null, 
			validateDays: null, 
			callBack: null,
			defaultDate: null,
			setFocus: null,
			autoBindDate: true
		};
		
		this.options = Object.extend(defaults, arguments[0] || {});		
		if(this.options.handle){
			this.handle = $(this.options.handle);
		}else{
			alert("handle error.");
			return;
		}
				
		this.calendar = $("HuShuaWebCalender");		
		this.caldocument = $frame("HuShuaWebCalender");		
		if(!this.caldocument || !this.caldocument.complete){
			alert("initialize error.");
			return;
		}		
		
		this.caldocument.defaultDate = this.options.defaultDate;
		if(this.options.beginDate) this.caldocument.beginDate = this.options.beginDate.AsDate();
		if(this.options.endDate) this.caldocument.endDate = this.options.endDate.AsDate();		
		this.caldocument.nextHandle = this.options.nextHandle;
		this.caldocument.nextField = this.options.nextField;
		this.caldocument.autoBindDate = this.options.autoBindDate;
		if(this.options.field){
			this.caldocument.date = $F(this.options.field).AsDate();
			this.caldocument.field = $(this.options.field);
		}else{
			if(this.handle.tagName.toLowerCase() == "input" || (this.handle.type.toLowerCase() == "text" && this.handle.type == "hidden")){
				this.options.field = this.handle;
				this.caldocument.field = this.options.field;
				this.caldocument.date = $F(this.options.field).AsDate();
			}
		}
		if(this.options.week) this.caldocument.enableWeek = this.options.week.split(",");
		if(this.options.validateDays) this.caldocument.validateDays = this.options.validateDays.split(",");
		this.caldocument.callBack = this.options.callBack;
		this.caldocument.setFocus = this.options.setFocus;
		this.caldocument.calObject = this;
		this.caldocument.Init();		
		
		this.eventHide   = this.Hide.bindAsEventListener(this)
		this.eventResize = this.Resize.bindAsEventListener(this)
		Event.observe(document, "click", this.eventHide);	
		Event.observe(window, "resize", this.eventResize);
		this.calendar.show();
		this.Resize();
		return this;
	},
	
	Resize : function(){
		var pageScroll = document.viewport.getScrollOffsets();
		var pos = this.handle.viewportOffset();	
		var left = pos.left;
		var top = pageScroll[1] + pos.top + this.handle.offsetHeight;
		var cWidth = parseFloat(this.calendar.getStyle("width").replace("px", ""));
		var cHeight = parseFloat(this.calendar.getStyle("height").replace("px", ""));
		var winWidth = document.viewport.getWidth();
		var winHeight = document.viewport.getHeight();
		if((left + cWidth) > winWidth){
			left -= (left + cWidth) - winWidth;
		}
		if((top + cHeight - pageScroll[1]) > winHeight){
			top = top - this.handle.offsetHeight - cHeight - 1;
		}else{
			top += 1;
		}
		this.calendar.setStyle({left: left + "px", top: top + "px"});
		return this;
	},
	
	Hide : function(){			
		if(this.calendar) this.calendar.hide();
		if(this.eventHide) Event.stopObserving(document, "click", this.eventHide);
		if(this.eventResize) Event.stopObserving(window, "resize", this.eventResize);
		return this;
	}
};